package com.relytosoft.mizar.media.camera.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.camera.api.entity.CameraType;
import com.relytosoft.mizar.media.camera.api.pojo.dto.CameraTypeDto;
import com.relytosoft.mizar.media.camera.api.pojo.vo.CameraTypeVo;
import com.relytosoft.mizar.media.camera.services.service.CameraTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author lj
 */
@Api(tags = {"设备分类"})
@RestController
@RequestMapping("camera/type")
public class CameraTypeController {

    @Resource
    private CameraTypeService cameraTypeService;

    @SaCheckPermission("camera:type:list")
    @OpLog(msg = "查询设备分类", opType = OpTypeEnum.SELECT)
    @ApiOperation("查询")
    @GetMapping("pagelist")
    public ResponseResult<Map<String, Object>> selectTypePageList(CameraTypeDto cameraTypeDto, PageQuery pageQuery) {
        return ResponseResult.ok(CommonUtils.getPageData(cameraTypeService.selectTypePageList(cameraTypeDto, pageQuery)));
    }

    @SaCheckPermission("camera:type:create")
    @OpLog(msg = "添加设备分类", opType = OpTypeEnum.INSERT)
    @ApiOperation("添加")
    @PostMapping({"/insert"})
    public ResponseResult<Void> insertCameraType(@Valid @RequestBody CameraTypeVo cameraTypeVo) {
        boolean add = cameraTypeService.add(cameraTypeVo);
        if (add) {
            return ResponseResult.ok("添加成功");
        } else {
            return ResponseResult.fail("添加失败，请检查该类型是否已存在");
        }
    }

    @SaCheckPermission("camera:type:del")
    @OpLog(msg = "删除设备分类", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除")
    @DeleteMapping
    public ResponseResult<Void> remove(@RequestBody Integer[] typeIds) {
        cameraTypeService.deleteCameraTypeByIds(typeIds);
        return ResponseResult.ok();
    }

    @SaCheckPermission("camera:type:update")
    @OpLog(msg = "修改设备分类", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody CameraTypeDto cameraTypeDto) {
        CameraType cameraType = BeanCopyUtils.copy(cameraTypeDto, CameraType.class);
        boolean b = cameraTypeService.updateCameraType(cameraType);
        if (b) {
            return ResponseResult.ok("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }


    @SaCheckPermission("camera:type:getinfo")
    @OpLog(msg = "查询设备分类详情", opType = OpTypeEnum.SELECT)
    @ApiOperation("详情")
    @GetMapping("{id}")
    public ResponseResult<CameraType> byId(@PathVariable Integer id) {
        return ResponseResult.ok(cameraTypeService.byId(id));
    }

}
